package org.jeecg.modules.payment.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.math3.analysis.function.Log;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.cuckoo.enums.PaymentStatusEnum;
import org.jeecg.modules.entity.CuPaymentOrders;
import org.jeecg.modules.payment.impl.PaymentBmwlImpl;
import org.jeecg.modules.service.ICuPaymentOrdersService;
import org.jeecg.modules.service.ICuVipCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * Bmw 支付通道
 *
 * @author: wei
 * @date: 2024年08月07日 11:30
 */
@Tag(name = "支付通道")
@RestController
@RequestMapping("/bmwl")
@Slf4j
public class PaymentBmwController {

    @Autowired
    private PaymentBmwlImpl paymentBmwlImpl;

    @Autowired
    private ICuPaymentOrdersService cuPaymentOrdersService;

    @Autowired
    private ICuVipCardService cuVipCardService;


    @Operation(summary = "支付结果通知")
    @GetMapping(value = "/notice")
    public String notice(@RequestParam Map<String, Object> params, HttpServletRequest req) {
        log.info("params:{}", params);
        if (ObjectUtil.isEmpty(params.get("trade_status")) || !params.get("trade_status").toString().equals("TRADE_SUCCESS")) {
            return "success";
        }
        if (ObjectUtil.isEmpty(params.get("out_trade_no"))) {
            return "success";
        }
        String out_trade_no = params.get("out_trade_no").toString();

        CuPaymentOrders cuPaymentOrders = new CuPaymentOrders();
        cuPaymentOrders.setOrderId(out_trade_no);

        QueryWrapper<CuPaymentOrders> queryWrapper = new QueryWrapper<>(cuPaymentOrders);
        cuPaymentOrders = cuPaymentOrdersService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(cuPaymentOrders)) {
            return "success";
        }
        // 不等于待处理直接退出
        if (!cuPaymentOrders.getPaymentStatus().equals(PaymentStatusEnum.PAYMEN_TSTATUS_PROCESSING)) {
            return "success";
        }

        BigDecimal money = new BigDecimal(params.get("money").toString());
        cuPaymentOrders.setPaidAmount(money);

        // 校验加密
        String sign = paymentBmwlImpl.generateSignature(params);
        if (!sign.equals(params.get("sign").toString())) {
            return "success";
        }

        // 业务处理
        asyncNotice(cuPaymentOrders);

        return "success";
    }


    /**
     * 多线程执行
     */
    @Async
    public CompletableFuture<String> asyncNotice(CuPaymentOrders cuPaymentOrders) {
        log.info("支付回调异步处理");
        try {
            cuVipCardService.payResult(cuPaymentOrders);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture("");
    }
}
